package com.vivz.baby.rest.client;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.vivz.baby.domain.Log;

@Configuration
@PropertySource("classpath:services.properties")
public class RestClient {
	
	// private static final String LOCAL_URL =
	// "http://localhost:7001/baby-rest-services/";
	// private static final String AWS_URL =
	// "http://default-environment-2xgz38vab8.elasticbeanstalk.com/baby-rest-services/";
	private static Date from;
	private static Date to;
	private static final List<Log> logs = new ArrayList<Log>();
	
	// @Resource(name = "services")
	// @Autowired
	// @Qualifier("services")
	// private Properties services;
	
	// @Bean
	// public static PropertyPlaceholderConfigurer properties() {
	// final PropertyPlaceholderConfigurer ppc = new
	// PropertyPlaceholderConfigurer();
	// final ClassPathResource[] resources = new ClassPathResource[] { new
	// ClassPathResource("services.properties") };
	// ppc.setLocations(resources);
	// ppc.setIgnoreUnresolvablePlaceholders(true);
	// return ppc;
	// }
	
	// @Value("#{services['rest.log.service.url']}")
	@Value("#{rest.log.service.url}")
	private String url;
	
	public static void main(final String[] args) {
		final ApplicationContext ctx = new AnnotationConfigApplicationContext(RestClient.class);
		final String url = ctx.getBean(RestClient.class).url;
		week(url + "log/get/dates");
	}
	
	private static void week(final String url) {
		
		// final String url = localUrl + "log/get/dates";
		System.out.println(url);
		final RestTemplate restTemplate = new RestTemplate();
		final List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
		messageConverters.add(new MappingJackson2HttpMessageConverter());
		
		final Map<String, Date> request = new LinkedHashMap<String, Date>();
		final Calendar c = Calendar.getInstance();
		to = c.getTime();
		c.add(Calendar.HOUR, -24 * 7);
		from = c.getTime();
		
		request.put("date1", from);
		request.put("date2", to);
		
		final List<?> body = restTemplate.postForObject(url + "?username=weekuser&", request, List.class);
		
		System.out.println(body);
	}
	
	private static void a() {
		final String url = "http://localhost:7001/baby-rest-services/log/add";
		final RestTemplate restTemplate = new RestTemplate();
		final List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
		messageConverters.add(new MappingJackson2HttpMessageConverter());
		
		final Map<String, Date> request = new LinkedHashMap<String, Date>();
		final Calendar c = Calendar.getInstance();
		to = c.getTime();
		c.add(Calendar.HOUR, -24 * 7);
		from = c.getTime();
		
		request.put("d1", from);
		request.put("d2", to);
		
		final Log newlog = new Log();
		final Log body = restTemplate.postForObject(url, newlog, Log.class);
		
		System.out.println(body);
	}
	
	private static void m() {
		final RestTemplate restTemplate = new RestTemplate();
		final Log log = restTemplate.getForObject("http://10.240.75.194:7001/baby-rest-services/log/get/3000", Log.class);
		System.out.println("Log:    " + log);
		
		final List<?> logs = restTemplate.getForObject("http://10.240.75.194:7001/baby-rest-services/log/get", List.class);
		System.out.println("Logs size:    " + logs.size());
	}
	
}
